// pages/support/support.js
var wxParser = require('../../wxParser/index');
Page({
  data:{
    loadHide: true,
    articleList: [],
    detailIds : {}, // article_id: content_id
    contentObj: {}, // article_id: content
    richTextId: {},
    
    pageNo : 0,  // 当前分页
    pageSize : 2, // 单页数量
    totalPage : 0, // 总页数
    totalNum : 0 // 总数量
  },
  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数
    var that = this;
    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          scrollHeight:res.windowHeight
        });
      }
    });
    getSupportList(this, 1);
  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示
  },
  bindDownload: function(){
    // 该方法绑定页面滑动到底部事件
    console.log("download");
    this.setData({
      loadHide:false
    });

    getSupportList(this, 0);
  },
  scroll: function(e){
    // 滚动事件
    this.setData({
      scrollTop: e.detail.scrollTop
    });
  },
  gotoContentDetail: function(e){
    var detailid = e.currentTarget.dataset.detailid;
    console.log(detailid);
    wx.navigateTo({
        url: '/pages/content/content?id='+detailid
    })
  }
})

var getSupportList = function(that, isRefresh){
  if(!isRefresh && that.data.pageNo != 0 && that.data.pageNo >= that.data.totalPage){
    that.setData({
        loadHide: true
    });
    return;
  }
  that.setData({
      loadHide: false
  });

  var pageNum = isRefresh ? 0 : that.data.pageNo; // 页码
  var limit = that.data.pageSize;
  var offset = limit * pageNum;
  var objects = {
    tableID: getApp().globalData.TABLE_ID_SUPPORT,
    limit: limit,
    offset: offset
  };

  wx.BaaS.getRecordList(objects).then( (res) => {
    // success
    console.log(res);
    that.setData({
      loadHide: true,
      pageNo: that.data.pageNo+1,
      totalPage: Math.ceil(res.data.meta.total_count/that.data.pageSize),
      totalNum : res.data.meta.total_count
    });
    
    console.log(res.data);
    if(isRefresh){
      // 重新加载
      for(var i = 0; i < res.data.objects.length; i++){
        if(!that.data.richTextId[res.data.objects[i].response.left_content_id]){
          that.data.richTextId[res.data.objects[i].response.left_content_id] = true;
          wx.BaaS.getContent({ richTextID: res.data.objects[i].response.left_content_id}).then( (leftres) => {
            that.data.richTextId[leftres.data.id] = "richText_"+leftres.data.id;
            that.setData({richTextId: that.data.richTextId});
            wxParser.parse({
              bind: 'richText.'+leftres.data.id,
              html: leftres.data.content,
              target: that,
              enablePreviewImage: false
            });
          }, (err) => {
              // err
          }); 
        }
        if(!that.data.richTextId[res.data.objects[i].response.image_content_id]){
          that.data.richTextId[res.data.objects[i].response.image_content_id] = true;
          wx.BaaS.getContent({ richTextID: res.data.objects[i].response.image_content_id}).then( (imgres) => {
            that.data.richTextId[imgres.data.id] = "richText_"+imgres.data.id;
            that.setData({richTextId: that.data.richTextId});
            wxParser.parse({
              bind: 'richText.'+imgres.data.id,
              html: imgres.data.content,
              target: that,
              enablePreviewImage: false
            });
          }, (err) => {
              // err
          }); 
        }
        if(!that.data.richTextId[res.data.objects[i].response.right_content_id]){
          that.data.richTextId[res.data.objects[i].response.right_content_id] = true;
          wx.BaaS.getContent({ richTextID: res.data.objects[i].response.right_content_id}).then( (rightres) => {
            that.data.richTextId[rightres.data.id] = "richText_"+rightres.data.id;
            that.setData({richTextId: that.data.richTextId});
            wxParser.parse({
              bind: 'richText.'+rightres.data.id,
              html: rightres.data.content,
              target: that,
              enablePreviewImage: false
            });
          }, (err) => {
              // err
          }); 
        }
        if(!that.data.richTextId[res.data.objects[i].response.center_content_id]){
          that.data.richTextId[res.data.objects[i].response.center_content_id] = true;
          wx.BaaS.getContent({ richTextID: res.data.objects[i].response.center_content_id}).then( (centerres) => {
            that.data.richTextId[centerres.data.id] = "richText_"+centerres.data.id;
            that.setData({richTextId: that.data.richTextId});
            wxParser.parse({
              bind: 'richText.'+centerres.data.id,
              html: centerres.data.content,
              target: that,
              enablePreviewImage: false
            });
          }, (err) => {
              // err
          }); 
        }   
      }
      that.setData({
        articleList: res.data.objects
      });
    }else{
      var articleList = that.data.articleList;
      for(var i=0; i<res.data.objects.length; i++){
        if(!that.data.richTextId[res.data.objects[i].response.left_content_id]){
          that.data.richTextId[res.data.objects[i].response.left_content_id] = true;
          wx.BaaS.getContent({ richTextID: res.data.objects[i].response.left_content_id}).then( (leftres) => {
            that.data.richTextId[leftres.data.id] = "richText_"+leftres.data.id;
            that.setData({richTextId: that.data.richTextId});
            wxParser.parse({
              bind: 'richText.'+leftres.data.id,
              html: leftres.data.content,
              target: that,
              enablePreviewImage: false
            });
          }, (err) => {
              // err
          }); 
        }
        if(!that.data.richTextId[res.data.objects[i].response.image_content_id]){
          that.data.richTextId[res.data.objects[i].response.image_content_id] = true;
          wx.BaaS.getContent({ richTextID: res.data.objects[i].response.image_content_id}).then( (imgres) => {
            that.data.richTextId[imgres.data.id] = "richText_"+imgres.data.id;
            that.setData({richTextId: that.data.richTextId});
            wxParser.parse({
              bind: 'richText.'+imgres.data.id,
              html: imgres.data.content,
              target: that,
              enablePreviewImage: false
            });
          }, (err) => {
              // err
          }); 
        }
        if(!that.data.richTextId[res.data.objects[i].response.right_content_id]){
          that.data.richTextId[res.data.objects[i].response.right_content_id] = true;
          wx.BaaS.getContent({ richTextID: res.data.objects[i].response.right_content_id}).then( (rightres) => {
            that.data.richTextId[rightres.data.id] = "richText_"+rightres.data.id;
            that.setData({richTextId: that.data.richTextId});
            wxParser.parse({
              bind: 'richText.'+rightres.data.id,
              html: rightres.data.content,
              target: that,
              enablePreviewImage: false
            });
          }, (err) => {
              // err
          }); 
        }
        if(!that.data.richTextId[res.data.objects[i].response.center_content_id]){
          that.data.richTextId[res.data.objects[i].response.center_content_id] = true;
          wx.BaaS.getContent({ richTextID: res.data.objects[i].response.center_content_id}).then( (centerres) => {
            that.data.richTextId[centerres.data.id] = "richText_"+centerres.data.id;
            that.setData({richTextId: that.data.richTextId});
            wxParser.parse({
              bind: 'richText.'+centerres.data.id,
              html: centerres.data.content,
              target: that,
              enablePreviewImage: false
            });
          }, (err) => {
              // err
          }); 
        }
        articleList.push(res.data.objects[i]);
      }

      var articleIds = {};
      var newArticleList = [];
      for (var i = 0; i < articleList.length; i++){
        if (typeof articleIds[articleList[i].id] == 'undefined'){
          articleIds[articleList[i].id] = true;
          newArticleList.push(articleList[i]);
        }
      }
      that.setData({
        articleList: newArticleList
      });
    }
  }, (err) => {
    // err
  });
}
